import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testserviceaccount:

    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/serviceaccount/1_get_serviceaccount_list.yaml'))
    def test_get_serviceaccount_list(self, serviceaccountinfo, get_env_info):
        # 获取serviceaccount列表接口
        baseUrl = get_env_info
        url = baseUrl + serviceaccountinfo['request']['url'].replace('{namespace}','default')
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/serviceaccount/2_create_serviceaccount.yaml'))
    def test_create_serviceaccount(self, serviceaccountinfo, get_env_info):
        # 创建serviceaccount接口
        baseUrl = get_env_info
        url = baseUrl + serviceaccountinfo['request']['url'].replace('{namespace}','default')
        method = serviceaccountinfo['request']['method']
        data = serviceaccountinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in serviceaccountinfo['name']:
            serviceaccountname=req.json()['metadata']['name']
            globals()['serviceaccountname']=serviceaccountname
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/serviceaccount/3_get_specific_serviceaccount.yaml'))
    def test_get_specific_serviceaccount(self, serviceaccountinfo, get_env_info):
        # 获取指定serviceaccount接口
        baseUrl = get_env_info
        url = baseUrl + serviceaccountinfo['request']['url'].replace('{namespace}','default').replace('{serviceaccountname}',globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in serviceaccountinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/serviceaccount/4_update_serviceaccount.yaml'))
    def test_update_serviceaccount(self, serviceaccountinfo, get_env_info):
        # 更新指定serviceaccount接口
        baseUrl = get_env_info
        url = baseUrl + serviceaccountinfo['request']['url'].replace('{namespace}','default').replace('{serviceaccountname}',globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        serviceaccountinfo['request']['data'] = globals()['updatereqbody']
        data = serviceaccountinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/serviceaccount/5_update_section_serviceaccount.yaml'))
    def test_update_section_serviceaccount(self, serviceaccountinfo, get_env_info):
        # 部分更新指定serviceaccount的接口
        baseUrl = get_env_info
        url = baseUrl + serviceaccountinfo['request']['url'].replace('{namespace}','default').replace('{serviceaccountname}',globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        data = serviceaccountinfo['request']['data']
        header = serviceaccountinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/serviceaccount/6_delete_specific_serviceaccount.yaml'))
    def test_delete_specific_serviceaccount(self, serviceaccountinfo, get_env_info):
        # 删除serviceaccount接口
        baseUrl = get_env_info
        url = baseUrl + serviceaccountinfo['request']['url'].replace('{namespace}','default').replace('{serviceaccountname}',globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']